package com.dabaozhan.modules.wx_app.facade.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dabaozhan.common.exception.RRException;
import com.dabaozhan.modules.wx_app.dao.RiskMapper;
import com.dabaozhan.modules.wx_app.dao.SiteOrderMapper;
import com.dabaozhan.modules.wx_app.dao.SiteOutRecordMapper;
import com.dabaozhan.modules.wx_app.entity.InventoryEntity;
import com.dabaozhan.modules.wx_app.entity.RiskEntity;
import com.dabaozhan.modules.wx_app.entity.SiteOrderEntity;
import com.dabaozhan.modules.wx_app.entity.SiteOutRecordEntity;
import com.dabaozhan.modules.wx_app.entity.StockResponseEntity;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;
import com.dabaozhan.modules.wx_app.facade.StockFacade;
import com.dabaozhan.modules.wx_app.service.InventoryService;

@Service("stockFacade")
public class StockFacadeImpl implements StockFacade{
	
	@Autowired
	private InventoryService inventoryService;
	
	@Autowired
	private SiteOrderMapper siteOrderMapper;
	
	@Autowired
	private SiteOutRecordMapper siteOutRecordMapper;
	
	@Autowired
	private RiskMapper riskMapper;

	@Override
	public List<StockResponseEntity> getStockList(String userId) {
		List<InventoryEntity> list =inventoryService.getAllList(userId);
		if(CollectionUtils.isEmpty(list)){
			throw new RRException(ResponseCodeEnum.ResponseCode_400.getCode(),ResponseCodeEnum.ResponseCode_400.getDescription());
		}
		//数据库不支持group by(经过测试),代码筛选出每个分类的最后一次pandian
		List<InventoryEntity> selectList = new ArrayList<InventoryEntity>();
		for(int i=0;i<list.size();i++){
			if(CollectionUtils.isEmpty(selectList)){
				selectList.add(list.get(i));
			}else{
				final String cateId = list.get(i).getCategoryId();
				final String pareId = list.get(i).getParentId();
				//如果selectList 里面已经包含了parentId 和 categroyId 则不做任何操作，反之添加到selectList
				List<InventoryEntity> pickList = selectList.stream().filter(s->(s.getCategoryId().equals(cateId)&&s.getParentId().equals(pareId))).collect(Collectors.toList());
				if(CollectionUtils.isEmpty(pickList)){
					selectList.add(list.get(i));
				}
			}
		}
		//覆盖list
		list = selectList;
		List<StockResponseEntity> stockList = new ArrayList<StockResponseEntity>();
		for(int i=0;i<list.size();i++){
			List<SiteOrderEntity> orderList = siteOrderMapper.getSiteOrderForStock(list.get(i).getUserId(), list.get(i).getCategoryId(), list.get(i).getCreateTime());
			BigDecimal totalInWeight = BigDecimal.ZERO;
			for(SiteOrderEntity order:orderList){
				totalInWeight= totalInWeight.add(order.getNetWeight());
			}
			List<SiteOutRecordEntity> recordList = siteOutRecordMapper.getRecordListForStock(list.get(i).getUserId(), list.get(i).getCategoryId(),list.get(i).getCreateTime());
			BigDecimal totalOutWeight = BigDecimal.ZERO;
			for(SiteOutRecordEntity record:recordList){
				totalOutWeight= totalOutWeight.add(record.getArriveNetWeight());
			}
			BigDecimal currentWeight = list.get(i).getTotalWeight().add(totalInWeight).subtract(totalOutWeight);
			StockResponseEntity stockResponse = new StockResponseEntity();
			stockResponse.setCategoryId(list.get(i).getCategoryId());
			stockResponse.setCategoryName(list.get(i).getCategoryName());
			stockResponse.setCurrentStock(String.valueOf(currentWeight));
			stockResponse.setLastInventory(String.valueOf(list.get(i).getTotalWeight()));
			stockResponse.setLastInventoryTime(list.get(i).getCreateTime());
			stockResponse.setParentId(list.get(i).getParentId());
			stockResponse.setPeriodExport(String.valueOf(totalOutWeight));
			stockResponse.setPeriodImport(String.valueOf(totalInWeight));
			RiskEntity riskEntity = riskMapper.getRiskWaring(userId,list.get(i).getParentId(),list.get(i).getCategoryId());
			if(null == riskEntity){
				stockResponse.setRiskStatus("1");
			}else{
				if(currentWeight.compareTo(riskEntity.getWarning())>0){
					stockResponse.setRiskStatus("2");
				}else{
					stockResponse.setRiskStatus("1");
				}
			}
			stockResponse.setUserId(userId);
			stockList.add(stockResponse);
		}
		//按照重量排序从大到小排序
		Comparator<StockResponseEntity> comparator = (h1, h2) -> new BigDecimal(h1.getCurrentStock()).compareTo(new BigDecimal(h2.getCurrentStock()));
		stockList.sort(comparator.reversed());
		return stockList;
	}

}
